using System;
using System.Text;
using System.Collections.ObjectModel;
using System.IO;
using System.Drawing;
using System.Xml.Serialization;
using System.Windows.Forms;

namespace BodySoundSimulator.CaseData
{
    public class CaseInformation
    {
        #region Fields
        float version1000 = 10.00f;
        Collection<AuscultationSiteInfo> frontViewAuscultationSites;
        Collection<AuscultationSiteInfo> backViewAuscultationSites;
        string caseID;
        string patientsClaim;
        string diseaseName;
        string soundName;
        #endregion

        #region Properties
        public float LatestVersion
        {
            get
            {
                return version1000;
            }
        }

        public Collection<AuscultationSiteInfo> FrontViewAuscultationSites
        {
            get { return frontViewAuscultationSites; }
            set { frontViewAuscultationSites = value; }
        }

        public Collection<AuscultationSiteInfo> BackViewAuscultationSites
        {
            get { return backViewAuscultationSites; }
            set { backViewAuscultationSites = value; }
        }

        public string CaseID
        {
            get { return caseID; }
            set { caseID = value; }
        }

        public string PatientsClaim
        {
            get { return patientsClaim; }
            set { patientsClaim = value; }
        }

        public string DiseaseName
        {
            get { return diseaseName; }
            set { diseaseName = value; }
        }

        public string SoundName
        {
            get { return soundName; }
            set { soundName = value; }
        }
        #endregion

        #region Constructors
        public CaseInformation()
        {
        }
        #endregion

        #region Public methods
        public void LoadBasicLungAndHeartAuscultationSites()
        {
            this.frontViewAuscultationSites = new Collection<AuscultationSiteInfo>();

            //load default front view auscultation sites
            AuscultationSiteInfo frontViewAuscultationSiteInfoOne = new AuscultationSiteInfo();
            frontViewAuscultationSiteInfoOne.Location = new PointF(-21.4f, 25.5f);
            AuscultationSiteInfo frontViewAuscultationSiteInfoTwo = new AuscultationSiteInfo();
            frontViewAuscultationSiteInfoTwo.Location = new PointF(16.5f, 23.5f);
            AuscultationSiteInfo frontViewAuscultationSiteInfoThree = new AuscultationSiteInfo();
            frontViewAuscultationSiteInfoThree.Location = new PointF(16.5f, 49.5f);
            AuscultationSiteInfo frontViewAuscultationSiteInfoFour = new AuscultationSiteInfo();
            frontViewAuscultationSiteInfoFour.Location = new PointF(17.5f, 70.5f);
            AuscultationSiteInfo frontViewAuscultationSiteInfoFive = new AuscultationSiteInfo();
            frontViewAuscultationSiteInfoFive.Location = new PointF(53.5f, 94.5f);
            AuscultationSiteInfo frontViewAuscultationSiteInfoSeven = new AuscultationSiteInfo();
            frontViewAuscultationSiteInfoSeven.Location = new PointF(-58, 15);
            AuscultationSiteInfo frontViewAuscultationSiteInfoEight = new AuscultationSiteInfo();
            frontViewAuscultationSiteInfoEight.Location = new PointF(-42, 55);
            AuscultationSiteInfo frontViewAuscultationSiteInfoNine = new AuscultationSiteInfo();
            frontViewAuscultationSiteInfoNine.Location = new PointF(-58, 82);
            AuscultationSiteInfo frontViewAuscultationSiteInfoTen = new AuscultationSiteInfo();
            frontViewAuscultationSiteInfoTen.Location = new PointF(-90, 105);
            AuscultationSiteInfo frontViewAuscultationSiteInfoElevan = new AuscultationSiteInfo();
            frontViewAuscultationSiteInfoElevan.Location = new PointF(49, 16);
            AuscultationSiteInfo frontViewAuscultationSiteInfoTwelve = new AuscultationSiteInfo();
            frontViewAuscultationSiteInfoTwelve.Location = new PointF(49, 52);
            AuscultationSiteInfo frontViewAuscultationSiteInfoThirteen = new AuscultationSiteInfo();
            frontViewAuscultationSiteInfoThirteen.Location = new PointF(91, 106);

            this.frontViewAuscultationSites.Add(frontViewAuscultationSiteInfoOne);
            this.frontViewAuscultationSites.Add(frontViewAuscultationSiteInfoTwo);
            this.frontViewAuscultationSites.Add(frontViewAuscultationSiteInfoThree);
            this.frontViewAuscultationSites.Add(frontViewAuscultationSiteInfoFour);
            this.frontViewAuscultationSites.Add(frontViewAuscultationSiteInfoFive);
            this.frontViewAuscultationSites.Add(frontViewAuscultationSiteInfoSeven);
            this.frontViewAuscultationSites.Add(frontViewAuscultationSiteInfoEight);
            this.frontViewAuscultationSites.Add(frontViewAuscultationSiteInfoNine);
            this.frontViewAuscultationSites.Add(frontViewAuscultationSiteInfoTen);
            this.frontViewAuscultationSites.Add(frontViewAuscultationSiteInfoElevan);
            this.frontViewAuscultationSites.Add(frontViewAuscultationSiteInfoTwelve);
            this.frontViewAuscultationSites.Add(frontViewAuscultationSiteInfoThirteen);


            //TODO: load back view auscultation sites
            this.backViewAuscultationSites = new Collection<AuscultationSiteInfo>();
            AuscultationSiteInfo backViewAuscultationSiteInfoOne = new AuscultationSiteInfo();
            backViewAuscultationSiteInfoOne.Location = new PointF(-35, 21);
            AuscultationSiteInfo backViewAuscultationSiteInfoTwo = new AuscultationSiteInfo();
            backViewAuscultationSiteInfoTwo.Location = new PointF(-35, 55);
            AuscultationSiteInfo backViewAuscultationSiteInfoThree = new AuscultationSiteInfo();
            backViewAuscultationSiteInfoThree.Location = new PointF(-35, 92);
            AuscultationSiteInfo backViewAuscultationSiteInfoFour = new AuscultationSiteInfo();
            backViewAuscultationSiteInfoFour.Location = new PointF(-53, 122);
            AuscultationSiteInfo backViewAuscultationSiteInfoFive = new AuscultationSiteInfo();
            backViewAuscultationSiteInfoFive.Location = new PointF(-76, 151);
            AuscultationSiteInfo backViewAuscultationSiteInfoSix = new AuscultationSiteInfo();
            backViewAuscultationSiteInfoSix.Location = new PointF(28, 21);
            AuscultationSiteInfo backViewAuscultationSiteInfoSeven = new AuscultationSiteInfo();
            backViewAuscultationSiteInfoSeven.Location = new PointF(28, 55);
            AuscultationSiteInfo backViewAuscultationSiteInfoEight = new AuscultationSiteInfo();
            backViewAuscultationSiteInfoEight.Location = new PointF(28, 92);
            AuscultationSiteInfo backViewAuscultationSiteInfoNine = new AuscultationSiteInfo();
            backViewAuscultationSiteInfoNine.Location = new PointF(48, 126);
            AuscultationSiteInfo backViewAuscultationSiteInfoTen = new AuscultationSiteInfo();
            backViewAuscultationSiteInfoTen.Location = new PointF(78, 152);

            this.backViewAuscultationSites.Add(backViewAuscultationSiteInfoOne);
            this.backViewAuscultationSites.Add(backViewAuscultationSiteInfoTwo);
            this.backViewAuscultationSites.Add(backViewAuscultationSiteInfoThree);
            this.backViewAuscultationSites.Add(backViewAuscultationSiteInfoFour);
            this.backViewAuscultationSites.Add(backViewAuscultationSiteInfoFive);
            this.backViewAuscultationSites.Add(backViewAuscultationSiteInfoSix);
            this.backViewAuscultationSites.Add(backViewAuscultationSiteInfoSeven);
            this.backViewAuscultationSites.Add(backViewAuscultationSiteInfoEight);
            this.backViewAuscultationSites.Add(backViewAuscultationSiteInfoNine);
            this.backViewAuscultationSites.Add(backViewAuscultationSiteInfoTen);
        }

        public void Serialize(BinaryWriter writer)
        {
            writer.Write(this.LatestVersion);
            SupportiveFunctions.SerializeString(writer, this.caseID);
            SupportiveFunctions.SerializeString(writer, this.patientsClaim);
            SupportiveFunctions.SerializeString(writer, this.diseaseName);
            SupportiveFunctions.SerializeString(writer, this.soundName);
            int count = this.frontViewAuscultationSites.Count;
            writer.Write(count);
            for (int index = 0; index < count; index++)
            {
                this.frontViewAuscultationSites[index].Serialize(writer);
            }
            count = this.backViewAuscultationSites.Count;
            writer.Write(count);
            for (int index = 0; index < count; index++)
            {
                this.backViewAuscultationSites[index].Serialize(writer);
            }
        }

        public void Deseriaize(BinaryReader reader)
        {
            float version = reader.ReadSingle();
            this.caseID = SupportiveFunctions.DeserializeString(reader);
            this.patientsClaim = SupportiveFunctions.DeserializeString(reader);
            this.diseaseName = SupportiveFunctions.DeserializeString(reader);
            this.soundName = SupportiveFunctions.DeserializeString(reader);
            int count = reader.ReadInt32();
            this.frontViewAuscultationSites = new Collection<AuscultationSiteInfo>();
            for (int index = 0; index < count; index++)
            {
                AuscultationSiteInfo siteInfo = new AuscultationSiteInfo();
                siteInfo.Deserialize(reader, version);
                this.frontViewAuscultationSites.Add(siteInfo);
            }
            count = reader.ReadInt32();
            this.backViewAuscultationSites = new Collection<AuscultationSiteInfo>();
            for (int index = 0; index < count; index++)
            {
                AuscultationSiteInfo siteInfo = new AuscultationSiteInfo();
                siteInfo.Deserialize(reader, version);
                this.backViewAuscultationSites.Add(siteInfo);
            }
        }

        public void XmlSerialize(string filePath)
        {
            CaseInformation caseInfo = this;
            if (this == null)
            {
                caseInfo = new CaseInformation();
            }
            StreamWriter streamWriter = null;
            try
            {
                streamWriter = new StreamWriter(filePath);
                XmlSerializer xmlSerializer = new XmlSerializer(typeof(CaseInformation));
                xmlSerializer.Serialize(streamWriter, caseInfo);
            }
            catch (System.IO.IOException)
            {
            }
            finally
            {
                if (streamWriter != null)
                {
                    streamWriter.Close();
                }
            }
        }

        public static CaseInformation XmlDeserialize(string filePath)
        {
            CaseInformation caseInfo = null;
            StreamReader reader = null;
            try
            {
                reader = new StreamReader(filePath);
                XmlSerializer deserializer = new XmlSerializer(typeof(CaseInformation));
                caseInfo = deserializer.Deserialize(reader) as CaseInformation;
            }
            catch
            {
                CaseInformation info = new CaseInformation();
                info.LoadBasicLungAndHeartAuscultationSites();
                return info;
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
            }
            return caseInfo;
        }
        #endregion
    }
}
